﻿using Autofac;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models.StationConfig;
using ZoneLine.Stations;

namespace ZoneLine.ViewModels
{
    public class HStationConfigViewModel : ViewModelBase
    {
        private IModelDAL _dal;

        public HStationConfigViewModel(IModelDAL dal)
        {
            _dal = dal;

            Task.Run(() =>
            {
                HOp010Config = _dal.GetModel<HOP010StationConfig>(p => true);
                if (HOp010Config == null)
                {
                    HOp010Config = new HOP010StationConfig();
                    _dal.AddModel(HOp010Config);
                }
            });

            Task.Run(() =>
            {
                HOp040Config = _dal.GetModel<HOP040StationConfig>(p => true);
                if (HOp040Config == null)
                {
                    HOp040Config = new HOP040StationConfig();
                    _dal.AddModel(HOp040Config);
                }
            });

            Task.Run(() =>
            {
                LoadPorts();
                PackageScanConfig = _dal.GetModel<PackageScanBarConfig>(p => true);
                if (PackageScanConfig == null)
                {
                    PackageScanConfig = new PackageScanBarConfig();
                    _dal.AddModel(PackageScanConfig);
                }
            });

            Task.Run(() =>
            {
                PrinterNames = GetLocalPrinter()?.ToList();
            });
        }

        public string[] GetLocalPrinter()
        {
            string[] ListPrinter = new string[PrinterSettings.InstalledPrinters.Count];
            //获取当前打印机
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                ListPrinter[i] = (PrinterSettings.InstalledPrinters[i].ToString());
            }
            return ListPrinter;
        }

        private List<string> _printerNames;
        /// <summary>
        /// 打印机列表名称
        /// </summary>
        public List<string> PrinterNames
        {
            get { return _printerNames; }
            set => SetProperty(ref _printerNames, value);
        }


        private HOP010StationConfig _hOp010Config;

        public HOP010StationConfig HOp010Config
        {
            get { return _hOp010Config; }
            set => SetProperty(ref _hOp010Config, value);
        }

        private HOP040StationConfig _hOp040Config;

        public HOP040StationConfig HOp040Config
        {
            get { return _hOp040Config; }
            set => SetProperty(ref _hOp040Config, value);
        }


        private PackageScanBarConfig _packageScanConfig;

        public PackageScanBarConfig PackageScanConfig
        {
            get { return _packageScanConfig; }
            set => SetProperty(ref _packageScanConfig, value);
        }

        private RelayCommand _saveCmd;

        public RelayCommand SaveCmd
        {
            get { return _saveCmd ?? (_saveCmd = new RelayCommand(Save)); }
        }

        private void Save()
        {
            try
            {
                _dal.UpdateModel<HOP010StationConfig>(p => true, p => new HOP010StationConfig
                {
                    ReadStartAddr = HOp010Config.ReadStartAddr,
                    AllowWorkAddr = HOp010Config.AllowWorkAddr,
                    EndUploadCodeAddr = HOp010Config.EndUploadCodeAddr,
                    EndUploadDataAddr = HOp010Config.EndUploadDataAddr,
                    Ip = HOp010Config.Ip,
                   
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<HOP040StationConfig>(p => true, p => new HOP040StationConfig
                {
                    ReadStartAddr = HOp040Config.ReadStartAddr,
                    AllowWorkAddr = HOp040Config.AllowWorkAddr,
                    EndUploadCodeAddr = HOp040Config.EndUploadCodeAddr,
                    EndUploadDataAddr = HOp040Config.EndUploadDataAddr,
                    Ip = HOp040Config.Ip,
                    PrinterName = HOp040Config.PrinterName,
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<PackageScanBarConfig>(p => true, p => new PackageScanBarConfig
                {
                    Port = PackageScanConfig.Port,
                    Printer = PackageScanConfig.Printer,
                    PrintTemplate = PackageScanConfig.PrintTemplate,
                });

            }
            catch (Exception ex)
            {

            }
        }

        private RelayCommand<string> _openFileCmd;

        public RelayCommand<string> OpenFileCmd
        {
            get { return _openFileCmd ?? (_openFileCmd = new RelayCommand<string>(OpenFile)); }
        }

        private void OpenFile(string obj)
        {
            FileDialog dia = new OpenFileDialog();
            var rlt = dia.ShowDialog();


        }

    
        #region 装箱

        private List<string> _portNames;

        public List<string> PortNames
        {
            get { return _portNames; }
            set => SetProperty(ref _portNames, value);
        }

        private RelayCommand _loadPortsCmd;

        public RelayCommand LoadPortsCmd
        {
            get { return _loadPortsCmd ?? (_loadPortsCmd = new RelayCommand(LoadPorts)); }
        }

        private void LoadPorts()
        {
            PortNames = SerialPort.GetPortNames().ToList();
        }

        #endregion
    }
}
